package org.example.lys.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.*;

/**
 * @author ; lidongdong
 * @Description
 * @Date 2020-03-12
 */
public class Base64ImageTools {
    public static String getImageStr(String imgFilePath) {
        Object var1 = null;

        byte[] data;
        try {
            InputStream in = new FileInputStream(imgFilePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }

        return new Base64().encodeAsString(data);
    }

    public static boolean generateImage(String imgStr, String imgFilePath) {
        if (imgStr == null) {
            return false;
        } else {
            try {
                byte[] bytes = (new Base64()).decode(imgStr);

                for(int i = 0; i < bytes.length; ++i) {
                    if (bytes[i] < 0) {
                        bytes[i] = (byte)(bytes[i] + 256);
                    }
                }

                OutputStream out = new FileOutputStream(imgFilePath);
                out.write(bytes);
                out.flush();
                out.close();
                return true;
            } catch (Exception var4) {
                throw new RuntimeException(var4);
            }
        }
    }
}
